/**
 * Created by zeopean on 2015/9/30.
 */
$(function(){
    var isUpdate    = 0 ;
    var Team        = {};

    /** 编辑项目团队 */
    $('.save_team').bind('click' , function(){
        var id = $('#project_id').val();
        if(id == 0){
            noty({text:'您的团队名不能为空',type:'error',timeout:1000});
            return false;
        }
        $('input[name=project_id]').val(id);
        var teamId  = $('#teamId').val();
        var url = EditUserTeams + teamId ;

        $.ajax({
            type:"post",
            url:url,
            dataType:"json",
            data:$('#form_team').serialize(),
            success:function(obj){
                if(obj.level === "success"){
                    noty({text:obj.message,type:obj.level,timeout:500});

                    $('.teams-detail').hide();
                    $('.add-team').show();

                    /** 获取更新后的内容 **/
                    $.ajax({
                        type: "get",
                        url: getTeamByProId,
                        dataType: "json",
                        data: {
                            'proId': id
                        },
                        success: function (obj) {
                            if (obj.level == 'success') {
                                var str = '';
                                for(var i in obj.data){
                                     str    += '<div class="team_msg">'
                                            +'<label>'+obj.data[i]['real_name']+'</label>'
                                            +'<img src="/api/images/team-logo/show/'+obj.data[i]['photo_id']+'" style="width:100px;height: 100px;">'
                                            +'<div><span class="edit_team"  data="'+obj.data[i]['id']+'">编辑</span>'
                                            +'<span class="del_team" data="'+obj.data[i]['id']+'">删除</span> </div>'
                                            +'</div>';
                                }
                                /** 进行页面替换 */
                                $('.team_msgs').html(str);
                            }
                        }
                    });
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:1000});
            }
        });
    });



    $(document).on('click' , '.add-team' , function(){
        var temp = $(this);
        $('.teams-detail :input').val('');
        $('.teams-detail').show();
        temp.hide();
    });


    /** 删除团队信息 del */
    $(document).on('click' , '.del_team' , function(){
        var proId  = $('#project_id').val();
        var teamId = $(this).attr('data');
        var tmp    = $(this);
        $.ajax({
            type:"get",
            url:delTeamByProIdAndTeamId,
            dataType:"json",
            data:{
                'proId' :   proId,
                'teamId':   teamId
            },
            success:function(obj){
                if(obj.level === "success"){
                    noty({text:obj.message,type:obj.level,timeout:500});
                    tmp.parents('div .team_msg').hide();
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:1000});
            }
        });

    });
    /** del end **/

    /** 编辑团队信息 edit */
    $(document).on('click' , '.edit_team' , function(){
        var proId  = $('#project_id').val();
        var teamId = $(this).attr('data');
        var tmp    = $(this);

        /** 获取对于的信息 */
        $.ajax({
            type:"get",
            url:getTeamByTeamId,
            dataType:"json",
            data:{
                'proId' :   proId,
                'teamId':   teamId
            },
            success:function(obj){
                if(obj.level === "success"){
                    /** 是否为创始人 **/
                    var isFounder = obj.data.is_founder;
                    if(isFounder == 1 ){
                        $('input[name=is_founder]').eq(0).attr('checked',true);
                    }
                    if(isFounder == 0){
                        $('input[name=is_founder]').eq(1).attr('checked',true);
                    }

                    $('input[name=photo_id]').val(obj.data.photo_id);

                    /** 图片地址 src*/
                    $('#team_photo_url').attr('src' , '/api/images/team-logo/show/'+obj.data.photo_id);
                    $('#teamId').val(obj.data.id);
                    $('input[name=realname]').val(obj.data.real_name);
                    $('input[name=position]').val(obj.data.position);
                    $('input[name=school]').val(obj.data.school);
                    $('textarea').text(obj.data.profile);
                    $('input[name=wechat]').val(obj.data.wechat);
                    $('input[name=weibo]').val(obj.data.weibo);
                    $('input[name=maimai]').val(obj.data.maimai);

                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:1000});
            }
        });

        $('.add-team').hide();
        $('.teams-detail').show();
        isUpdate = 1;
    });
    /** end edit**/

    /** end */


});

